/**
 *上海交大-鹏越惊虹
 */

package org.infosec.lc.exercises.threads;


/**
 * @Title: ThreadExample.java
 * @Package org.effective.Chapter10.Item66.ThreadExample
 * @Description: TODO
 * @author 林超
 * @date Apr 15, 2013 4:21:44 PM
 * @version V1.0
 */
public class ThreadExample implements Runnable {

	public ThreadExample() {
		new ThreadTest(this, "Test1").start();
		new ThreadTest(this, "Test2").start();
	}

	public static void main(String[] args) {
		new ThreadExample();
	}
	
	public void run() {
		ThreadTest ct = (ThreadTest) Thread.currentThread();
		try {
			if (!ct.getName().equalsIgnoreCase("test1")) {
				synchronized (this) {
					wait();
				}
			}
			
			while(true){
				System.out.println("@time in thread " + ct.getName() + " = " + ct.addTime());
				if(ct.getTime()%2==0){
					synchronized (this) {
						System.out.println("******************************************");
						notifyAll();
						if(ct.getTime() == 10){
							break;
						}
						wait();
					}
				}
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}

class ThreadTest extends Thread {
	private int time;

	public ThreadTest(Runnable r, String name) {
		super(r, name);
	}

	public int getTime() {
		return time;
	}

	public int addTime() {
		return ++this.time;
	}
}

/** Code End */
